package com.example.xjttoboxuegu.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xjttoboxuegu.R;
import com.example.xjttoboxuegu.util.MD5Util;

public class RegisterActivity extends AppCompatActivity {
    private TextView tv_main_title;
    private TextView tv_back;
    private EditText et_user_name, et_pwd, et_pwd_again;
    private Button register;
    private String userName, pwd,pwdAgain;
    private RelativeLayout rl_title_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置页面布局
        setContentView(R.layout.activity_register);
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    private void init() {
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_main_title.setText("注册");
        tv_back = findViewById(R.id.tv_back);
        rl_title_bar = findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.TRANSPARENT);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterActivity.this.finish();
            }
        });
        register = findViewById(R.id.btn_register);
        et_user_name = findViewById(R.id.et_user_name);
        et_pwd = findViewById(R.id.et_pwd);
        et_pwd_again = findViewById(R.id.et_pwd_again);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getEditString();
                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(RegisterActivity.this,"用户名不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(RegisterActivity.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(pwdAgain)) {
                    Toast.makeText(RegisterActivity.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }else if (!pwd.equals(pwdAgain)) {
                    Toast.makeText(RegisterActivity.this,"两次密码不一致！",Toast.LENGTH_SHORT).show();
                    return;
                }else if (idExistUserName(userName)) {
                    Toast.makeText(RegisterActivity.this,"此用户名已存在！",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                    saveRegisterInfo(userName,pwd);
                    Intent data = new Intent();
                    data.putExtra("userName",userName);
                    setResult(RESULT_OK,data);
                    RegisterActivity.this.finish();
                }

            }
        });
    }

    /**
     * 获取注册界面控件值
     */
    private void getEditString() {
        userName = et_user_name.getText().toString().trim();
        pwd = et_pwd.getText().toString().trim();
        pwdAgain = et_pwd_again.getText().toString().trim();
    }

    /**
     * 从SharePreferences判断用户名是否存在
     * @param userName
     * @return
     */
    private boolean idExistUserName(String userName) {
        boolean hasUserName = false;
        SharedPreferences info = getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spPwd = info.getString(userName,"");
        if (!TextUtils.isEmpty(spPwd)) {
            hasUserName = true;
        }
        return hasUserName;
    }

    /**
     * 将注册信息保存到SharePreferences
     * @param userName
     * @param pwd
     */
    private void saveRegisterInfo(String userName, String pwd) {
        String md5Pwd = MD5Util.md5(pwd);
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(userName,md5Pwd);
        editor.commit();
    }

}